package com.jzh.cmhSystem.controller;


import com.jzh.cmhSystem.biz.*;
import com.jzh.cmhSystem.entity.*;
import com.jzh.cmhSystem.utils.ThreadLocalUtil;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


import com.jzh.cmhSystem.biz.UserAndDetailService;
import com.jzh.cmhSystem.biz.UserService;
import com.jzh.cmhSystem.entity.Result;
import com.jzh.cmhSystem.entity.UserAndDetail;

import java.util.Map;


//@Tag(name = "企业用户相关", description = "这是描述")
@Api(tags = "企业管理员相关", value = "这是描述")
@RestController
@RequestMapping("/api/enterprise")
//@Slf4j
//请求值校验
public class EnterpriseController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserAndDetailService userAndDetailService;

    @Operation(summary = "获取用户本人信息")
    @GetMapping("/info")
    public Result<List<UserAndDetail>> getMyInfoById() {
        Map<String, Object> map = ThreadLocalUtil.get();
        int id = (int) map.get("id");
        return Result.success(userAndDetailService.getUserDetailByUserId(id));
    }

    @Operation(summary = "获取用户信息")
    @GetMapping("/get_user_info_by_id")
    public Result<List<UserAndDetail>> getUserInfoById(@RequestParam String userId) {
        return Result.success(userAndDetailService.getUserDetailByUserId(Integer.parseInt(userId)));
    }

    @Operation(summary = "修改本人信息")
    @PutMapping("/update_my_info")
    public Result updateMyInfo(@RequestBody UserAndDetail userAndDetail) {
        Map<String, Object> map = ThreadLocalUtil.get();
        int id = (int) map.get("id");
        userAndDetail.setUserId(id);
        userAndDetailService.updateInfo(userAndDetail);
        return Result.success("");
    }

    @Operation(summary = "修改用户信息")
    @PutMapping("/update_user_info")
    public Result updateUserInfo(@RequestBody UserAndDetail userAndDetail) {
        User user = new User();
        user.setRole(userAndDetail.getRole());
        user.setId(userAndDetail.getUserId());
        userService.updateById(user);

        Section section = sectionService.getSectionById(userAndDetail.getSectionId());
        userAndDetail.setEnterpriseId(section.getEnterpriseId());
        userAndDetailService.updateInfoById(userAndDetail);
        return Result.success("");

    }

    /*    @Operation(summary = "添加用户")
        @PostMapping("/addUser")
        public Result addUser(
                @RequestBody UserAndDetail userAndDetail
        ) {
            String username = userAndDetail.getUsername();
            String password = userAndDetail.getPassword().trim();
            if (password.isEmpty()) {
                password = "123456";
            }
            User u = userService.findUserByName(username);
            if (u == null) {
                //没有占用 注册初始化信息
                int userId = userService.register(username, password, userAndDetail.getRole());

                userAndDetail.setPassword(null);
                userAndDetail.setUserId(userId);

                userAndDetailService.addDetail(userAndDetail);
                return Result.success();
            } else {
                //占用
                return Result.error("用户名已被占用");
            }
        }*/
    @Operation(summary = "添加用户")
    @PostMapping("/addUser")
    public Result addUser(
            @RequestBody UserAndDetail userAndDetail
    ) {
        String username = userAndDetail.getUsername();
        String password = userAndDetail.getPassword().trim();
        if (password.isEmpty()) {
            password = "123456";
        }
        User u = userService.findUserByName(username);
        if (u == null) {
            //没有占用 注册初始化信息
            int userId = userService.register(username, password, userAndDetail.getRole());


            Map<String, Object> map = ThreadLocalUtil.get();
            int enUserId = (int) map.get("id");
            UserAndDetail seUserAndDetail = userAndDetailService.getUserDetailByUserId(enUserId).get(0);
            userAndDetail.setEnterpriseId(seUserAndDetail.getEnterpriseId());

            if(userAndDetail.getSectionId() != null){
                Section section = sectionService.getSectionById(userAndDetail.getSectionId());
                if (!Objects.equals(section.getEnterpriseId(), seUserAndDetail.getEnterpriseId())) {
                    return Result.error("你无权操作改部门");
                }
            }

            userAndDetail.setUserId(userId);

            userAndDetailService.addDetail(userAndDetail);
            return Result.success();
        } else {
            //占用
            return Result.error("用户名已被占用");
        }
    }

    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private SectionService sectionService;

    @Operation(summary = "本企业部门列表查询")
    @GetMapping("/getEnterprise")
    public Result<List<Enterprise>> getEnterprise(@RequestParam(required = false, defaultValue = "") String name) {
        Map<String, Object> map = ThreadLocalUtil.get();
        int userId = (int) map.get("id");
        UserAndDetail userAndDetail = userAndDetailService.getUserDetailByUserId(userId).get(0);

        if (userAndDetail.getEnterpriseId() == null) {
            return Result.error("您没有注册公司");
        }
        return Result.success(enterpriseService.getEnterprisesWithSectionsBySectionNameAndEnterpriseId(name, userAndDetail.getEnterpriseId()));
    }

    @Operation(summary = "本企业普通用户列表查询")
    @GetMapping("/get_user_list_by_name")
    public Result<List<UserAndDetail>> getUserListByName(
//            联系人，电话，租户名称
            @RequestParam(required = false, defaultValue = "") String nickname,
            @RequestParam(required = false, defaultValue = "") String phone,
            @RequestParam(required = false, defaultValue = "") String name
    ) {
        Map<String, Object> map = ThreadLocalUtil.get();
        int userId = (int) map.get("id");
        List<UserAndDetail> userAndDetails = userAndDetailService.getUserDetailByUserId(userId);

        if (userAndDetails.isEmpty() || userAndDetails.get(0).getEnterpriseId() == null) {
            return Result.error("您没有注册公司");
        }
        return Result.success(userAndDetailService.getUserListByRoleAndName(nickname, phone, name, null, userAndDetails.get(0).getEnterpriseId()));
    }
}
